package com.jason.product.service;

import com.jason.common.dubboapi.IProductServiceApi;
import com.jason.product.mapper.ProductMapper;
import io.seata.rm.tcc.api.BusinessActionContext;
import org.apache.commons.lang3.RandomUtils;
import org.apache.dubbo.config.annotation.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Service(retries = 0,timeout = 3000)
public class ProductService implements IProductServiceApi {
    private static final Logger logger = LoggerFactory.getLogger(ProductService.class);
    @Autowired
    private ProductMapper productMapper;

    @Override
    @Transactional
    public String reduceStore(BusinessActionContext businessActionContext, Long productId) {
        try {
            logger.info("reduceStore begin");
            productMapper.reduceProduct(productId);
        }catch (Exception ex) {
            System.err.println(ex);
            throw ex;
        }

        /*if(System.currentTimeMillis()%2 == 0) {
            throw  new RuntimeException("测试回滚");
        }*/
        return "success";

    }

    @Override
    public boolean commitProduct(BusinessActionContext businessActionContext) {
        logger.info("======================commit product");
        return true;
    }

    @Override
    public boolean rollbackProduct(BusinessActionContext businessActionContext) {
        logger.info("======================rollback product");
        return true;
    }

    @Override
    public String testThrowException(){
        logger.info("======================testThrowException");
        if(true) {
            throw  new RuntimeException("测试抛出异常的情况");
        }
         return "success";
    }
}
